package com.eboss.sbs.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.mytools.tool.utils.ScreenUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

import java.util.ArrayList;

public class PicturesShow extends BaseActivity {
	
	private LinearLayout picture_list;  
	Bitmap bitmap = null;
	LinearLayout.LayoutParams lp = null;
	int width = 0;
    int height = 0;
    
    private ArrayList<String> imgurls;
    private String title;
     
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_pictures_show);
		super.onCreate(savedInstanceState);
		showView(R.id.titleback_btn_back, true);
		title = getIntent().getStringExtra("title");
		imgurls = (ArrayList<String>) getIntent().getSerializableExtra("imgurls");
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		setTvText(R.id.tv_title, title);
		width = ScreenUtils.getScreenWidth(PicturesShow.this);
		picture_list=(LinearLayout) findViewById(R.id.picture_list);
		showPictures();
	}
	
	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.titleback_btn_back){finish();}
		super.onClick(v);
	}
	
	private void showPictures(){
        
		if(imgurls==null||imgurls.size()==0)return;
		int size = imgurls.size();
		for (int i = 0; i < size; i++) {
			String url = imgurls.get(i);

          final  View view = getLayoutInflater().inflate(R.layout.item_pictures_show, null);
          final  ImageView iv = (ImageView)view.findViewById(R.id.image);
            
          ImageLoader.getInstance().loadImage(url, new ImageLoadingListener() {
				@Override
				public void onLoadingStarted(String arg0, View arg1) {}
				@Override
				public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {}
				@Override
				public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
					bitmap = arg2;
					lp = new LinearLayout.LayoutParams(width, height = bitmap.getHeight()*width/bitmap.getWidth());
		            iv.setLayoutParams(lp);
		            iv.setMaxWidth(width);
		            iv.setMaxHeight(height);
		            iv.setImageBitmap(bitmap);
				}
				@Override
				public void onLoadingCancelled(String arg0, View arg1) {}
				
			});
            
            picture_list.addView(view);
            
        }
		
	}
	
}
